package com.CMUBiS.util;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.regex.Pattern;

import javax.servlet.http.HttpServletRequest;

import com.CMUBiS.Data.Error;
import com.CMUBiS.Data.GlobalErrors;
import com.CMUBiS.Data.GlobalVars;

/**
 * This class checks the parameters sent to the pages. This is in a static class as lots of the pages take
 * the same parameters, so it saves each page having to implement them
 * 
 * The parameters since we made the client generator, so we know what we are passing. However, in real life people
 * will try to break and hack whatever you make
 * 
 * @author Andy (andrewtu@cmu.edu, turner.andy@gmail.com)
 * @version 0.1
 * @since 0.1
 * @date 04/05/2011
 */
public class CheckInputs {
	private static final String EMPTY = "";
	
	// The RegEx to see if the zip and email are valid
	private static final Pattern zipFormatCheck = Pattern.compile("\\d{5}(-\\d{4})?",Pattern.CASE_INSENSITIVE);
	private static final Pattern emailFormatCheck = Pattern.compile("^[\\w\\-]([\\.\\w])+[\\w]+@([\\w\\-]+\\.)+[A-Z]{2,4}$",Pattern.CASE_INSENSITIVE);
	   
	
	/**
	 * Makes sure the userID is valid for use in the system. It must be present and greater than zero.
	 * Returns the userID if valid, otherwise it throws an exception that describes the problem
	 * 
	 * @param request
	 * @return int of the userID
	 * @throws Exception
	 * @author Andy (andrewtu@cmu.edu, turner.andy@gmail.com)
	 */
	public static int checkUserID(HttpServletRequest request) throws Error{
		String userID = request.getParameter("userID");
		int userIDInt = 0;
		
		//See if the userID is present
		if(userID == null){
			throw GlobalErrors.userIDNotPresent;
		}
		// See if the userID is an empty string
		else if(userID.equals(EMPTY)){
			throw GlobalErrors.userIDEmpty;
		}
		else{
			// make sure the userID is a number
			try{
				userIDInt = Integer.parseInt(userID);
				
				// make sure the userID is positive
				if(userIDInt <0)
					throw GlobalErrors.userIDLessThanZero;
			}
			catch(NumberFormatException e){
				throw GlobalErrors.userIDNotAnInteger;
			}
		}
		
		// If we made it here then everything is okay, pass the userID back
		return userIDInt;
	}
	
	/**
	 * Checks that the authToken passed is in the correct format. Returns the authToken if valid, otherwise an exception
	 * 
	 * @param request
	 * @return String of the authToken
	 * @throws Exception
	 * @author Andy (andrewtu@cmu.edu, turner.andy@gmail.com)
	 */
	public static String checkAuthToken(HttpServletRequest request) throws Error{
		String authToken = request.getParameter("authToken");
		
		//See if the authToken is present
		if(authToken == null){
			throw GlobalErrors.authTokenNotPresent;
		}
		// See if the userID is an empty string
		else if(authToken.equals(EMPTY)){
			throw GlobalErrors.authTokenEmpty;
		}
		
		return authToken;
	}
	
	/**
	 * Checks the suppressOutput option. The suppressOutput option tells the page to clear the output writer
	 * before it returns the page, this reduces the amount of network traffic. This might not be too useful
	 * in the final system as the client generator will need the page data for most pages
	 * 
	 *
	 * 
	 * @param request The request to read the parameter from
	 * @return boolean true if we are to suppress the output, false otherwise
	 * @author Andy (andrewtu@cmu.edu, turner.andy@gmail.com)
	 */
	//TODO: Andy: is it more efficient to use the Boolean object?
	public static boolean checkSuppressOutput(HttpServletRequest request){
		String suppressOutput = request.getParameter("suppressOutput");
			
		// If suppressOutput is set to '1' then return true as we are to suppress the page output
		if(suppressOutput != null && suppressOutput.equals("1"))
			return true;
			
		// Otherwise false, and we print the page output
		else return false;
	}
	
	/**
	 * Gets the processing loop parameter. The processing loop parameter can be used to increase to increase the
	 * processing on the app tier without having to increase load on the DB.
	 * 
	 * @param request The request to read the parameter from
	 * @return int The number of times to loop the processing of the page
	 * @author Andy (andrewtu@cmu.edu, turner.andy@gmail.com)
	 */
	public static int checkProcessingLoop(HttpServletRequest request){
		String processingLoop = request.getParameter("processingLoop");
		
		// Check that the loop parameter is present
		if(processingLoop != null){
			try{
				int processingLoopInt = Integer.parseInt(processingLoop);
				
				// Return the number of times to loop the processing
				return processingLoopInt;
			}
			catch(NumberFormatException e){
			}
		}
		
		// The default number of times to loop is zero
		return 0;
	}
	
	/**
	 * Gets the categoryID. Returns the categoryID if present and is a positive integer, otherwise throws an error
	 * 
	 * @param request The request to read the parameter from
	 * @return int The categoryID number
	 * @throws Error
	 * @author Andy (andrewtu@cmu.edu, turner.andy@gmail.com)
	 */
	public static int checkCategoryID(HttpServletRequest request) throws Error{
		String categoryID = request.getParameter("categoryID");
		int categoryIDInt = 0;
		
		//See if the categoryID is present
		if(categoryID == null){
			throw GlobalErrors.categoryIDNotPresent;
		}
		// See if the categoryID is an empty string
		else if(categoryID.equals(EMPTY)){
			throw GlobalErrors.categoryIDEmpty;
		}
		else{
			// make sure the categoryID is a number
			try{
				categoryIDInt = Integer.parseInt(categoryID);
				
				// make sure the categoryID is positive
				if(categoryIDInt <0)
					throw GlobalErrors.categoryIDLessThanZero;
			}
			catch(NumberFormatException e){
				throw GlobalErrors.categoryIDNotAnInteger;
			}
		}
		
		// If we made it here then everything is okay, pass the categoryID back
		return categoryIDInt;
	}
	
	/**
	 * Checks the page number. Returns the page number if a positive integer, otherwise throws an error
	 * 
	 * @param requestThe request to read the parameter from
	 * @return int The current page number
	 * @throws Error
	 * @author Andy (andrewtu@cmu.edu, turner.andy@gmail.com)
	 */
	public static int checkPageNumber(HttpServletRequest request) throws Error{
		String pageNo = request.getParameter("pageNo");
		int pageNoInt = 0;
		
		//See if the page is present
		if(pageNo == null){
			throw GlobalErrors.pageNoNotPresent;
		}
		// See if the page is an empty string
		else if(pageNo.equals(EMPTY)){
			throw GlobalErrors.pageNoEmpty;
		}
		else{
			// make sure the page is a number
			try{
				pageNoInt = Integer.parseInt(pageNo);
				
				// make sure the page is positive
				if(pageNoInt <0)
					throw GlobalErrors.pageNoLessThanZero;
			}
			catch(NumberFormatException e){
				throw GlobalErrors.pageNoNotAnInteger;
			}
		}
		
		// If we made it here then everything is okay, pass the page back
		return pageNoInt;
	}
	
	/**
	 * Gets the items per page parameter. Returns itemsPP if a positive integer, otherwise throws an error
	 * 
	 * @param request The request to read the parameter from
	 * @return int The nuber of items per page
	 * @throws Error
	 * @author Andy (andrewtu@cmu.edu, turner.andy@gmail.com)
	 */
	public static int checkItemsPerPage(HttpServletRequest request) throws Error{
		String itemsPP = request.getParameter("itemsPP");
		int itemsPPInt = 0;
		
		//See if the items per page is present
		if(itemsPP == null){
			throw GlobalErrors.itemsPPNotPresent;
		}
		// See if the items per page is an empty string
		else if(itemsPP.equals(EMPTY)){
			throw GlobalErrors.itemsPPEmpty;
		}
		else{
			// make sure the items per page is a number
			try{
				itemsPPInt = Integer.parseInt(itemsPP);
				
				// make sure the items per page is positive
				if(itemsPPInt <0)
					throw GlobalErrors.itemsPPLessThanZero;
			}
			catch(NumberFormatException e){
				throw GlobalErrors.itemsPPNotAnInteger;
			}
		}
		
		// If we made it here then everything is okay, pass the items per page
		return itemsPPInt;
	}
	
	/**
	 * Check that the username passed is correct and is not already present in the database
	 * 
	 * @param request The request to read the parameter from
	 * @return String of the username if valid
	 * @throws Error
	 * @author Andy (andrewtu@cmu.edu, turner.andy@gmail.com)
	 */
	public static String checkUsername(HttpServletRequest request) throws Error{
		String username = request.getParameter("username");

		//See if the username is present
		if(username == null){
			throw GlobalErrors.usernameNotPresent;
		}
		// See if the username is an empty string
		else if(username.equals(EMPTY)){
			throw GlobalErrors.usernameEmpty;
		}
		else{
			// Make sure the username is not already taken
			if(GlobalVars.db.usernameExists(username)){
				throw GlobalErrors.usernameTaken;
			}
		}
		
		// If we made it here then everything is okay, pass the the username
		return username;
	}
	
	/**
	 * Checks that the email addresses are the same and not already present in the database, otherwise it throws an error
	 * 
	 * @param request The request to read the parameter from
	 * @return string of the email address if they are both the same
	 * @throws Error
	 * @author Andy (andrewtu@cmu.edu, turner.andy@gmail.com)
	 */
	public static String checkEmailsNotPresent(HttpServletRequest request) throws Error{
		return checkEmailsNotPresent(request, true);
	}
	
	/**
	 * 
	 * 
	 * @param request request The request to read the parameter from
	 * @param checkDB true if we should check the DB for the address
	 * @return string of the email address if they are both the same
	 * @throws Error
	 * @author Andy (andrewtu@cmu.edu, turner.andy@gmail.com)
	 */
	private static String checkEmailsNotPresent(HttpServletRequest request, boolean checkDB) throws Error{
		String email1 = request.getParameter("email1");
		String email2 = request.getParameter("email2");
		
		//See if the e-mail address is present
		if(email1 == null){
			throw GlobalErrors.emailNotPresent;
		}
		// See if the e-mail address is an empty string
		else if(email1.equals(EMPTY)){
			throw GlobalErrors.emailEmpty;
		}
		else{
			// make sure the e-mail addresses are the same
			if(!email1.equals(email2))
				throw GlobalErrors.emailDifferent;
			
			// Make sure the email format is correct
			if(!emailFormatCheck.matcher(email1).matches())
				throw GlobalErrors.emailNotVaild;
			
			// Make sure the e-mail address is not already taken
			if(checkDB && GlobalVars.db.emailExists(email1))
				throw GlobalErrors.emailTaken;
		}
		
		// If we made it here then everything is okay, pass the the e-mail address
		return email1;
	}
	
	/**
	 * Checks that the email addresses are the same, otherwise it throws an error
	 * 
	 * @param request The request to read the parameter from
	 * @return string of the email address if they are both the same
	 * @throws Error
	 * @author Andy (andrewtu@cmu.edu, turner.andy@gmail.com)
	 */
	public static String checkEmailsVaild(HttpServletRequest request) throws Error{
		return checkEmailsNotPresent(request, false);
	}
	
	/**
	 * Gets the bid amount. Returns the bid amount if present and is a positive float, otherwise throws an error
	 * 
	 * @param request The request to read the parameter from
	 * @return float The bid amount
	 * @throws Error
	 * @author Andy (andrewtu@cmu.edu, turner.andy@gmail.com)
	 */
	public static float checkBid(HttpServletRequest request) throws Error{
		String bid = request.getParameter("bid");
		float bidF = 0;
		
		//See if the bid is present
		if(bid == null){
			throw GlobalErrors.bidNotPresent;
		}
		// See if the bid is an empty string
		else if(bid.equals(EMPTY)){
			throw GlobalErrors.bidEmpty;
		}
		else{
			// make sure the bid is a number
			try{
				bidF = Float.parseFloat(bid);
				
				// make sure the bid is positive
				if(bidF < 0.0f)
					throw GlobalErrors.bidLessThanZero;
			}
			catch(NumberFormatException e){
				throw GlobalErrors.bidNotAFloat;
			}
		}
		
		// If we made it here then everything is okay, pass the categoryID back
		return bidF;
	}
	
	/**
	 * Gets the max bid amount. Returns the max bid amount if present and is a positive float, otherwise throws an error
	 * 
	 * @param request The request to read the parameter from
	 * @return float The max bid amount
	 * @throws Error
	 * @author Andy (andrewtu@cmu.edu, turner.andy@gmail.com)
	 */
	public static float checkMaxBid(HttpServletRequest request) throws Error{
		String maxBid = request.getParameter("maxBid");
		float maxBidF = 0;
		
		//See if the max bid is present
		if(maxBid == null){
			throw GlobalErrors.maxBidNotPresent;
		}
		// See if the max bid is an empty string
		else if(maxBid.equals(EMPTY)){
			throw GlobalErrors.maxBidEmpty;
		}
		else{
			// make sure the max bid is a number
			try{
				maxBidF = Float.parseFloat(maxBid);
				
				// make sure the bid is positive
				if(maxBidF < 0.0f)
					throw GlobalErrors.maxBidLessThanZero;
			}
			catch(NumberFormatException e){
				throw GlobalErrors.maxBidNotAFloat;
			}
		}
		
		// If we made it here then everything is okay, pass the categoryID back
		return maxBidF;
	}
	
	/**
	 * Gets the quantity parameter. Returns quantity if a positive integer, otherwise throws an error
	 * 
	 * @param request The request to read the parameter from
	 * @return int The nuber of items
	 * @throws Error
	 * @author Andy (andrewtu@cmu.edu, turner.andy@gmail.com)
	 */
	public static int checkQuantity(HttpServletRequest request) throws Error{
		String quantity = request.getParameter("quantity");
		int quantityInt = 0;
		
		//See if the quantity is present
		if(quantity == null){
			throw GlobalErrors.quantityNotPresent;
		}
		// See if the quantity is an empty string
		else if(quantity.equals(EMPTY)){
			throw GlobalErrors.quantityEmpty;
		}
		else{
			// make sure the quantity is a number
			try{
				quantityInt = Integer.parseInt(quantity);
				
				// make sure the quantity is positive
				if(quantityInt <0)
					throw GlobalErrors.quantityLessThanZero;
			}
			catch(NumberFormatException e){
				throw GlobalErrors.quantityNotAnInteger;
			}
		}
		
		// If we made it here then everything is okay, pass the items per page
		return quantityInt;
	}
	
	/**
	 * Gets the itemID parameter. Returns itemID if a positive integer, otherwise throws an error
	 * 
	 * @param request The request to read the parameter from
	 * @return int The nuber of items
	 * @throws Error
	 * @author Andy (andrewtu@cmu.edu, turner.andy@gmail.com)
	 */
	public static int checkItemID(HttpServletRequest request) throws Error{
		String itemID = request.getParameter("itemID");
		int itemIDInt = 0;
		
		//See if the itemID is present
		if(itemID == null){
			throw GlobalErrors.itemIDNotPresent;
		}
		// See if the itemID is an empty string
		else if(itemID.equals(EMPTY)){
			throw GlobalErrors.itemIDEmpty;
		}
		else{
			// make sure the itemID is a number
			try{
				itemIDInt = Integer.parseInt(itemID);
				
				// make sure the itemID is positive
				if(itemIDInt <0)
					throw GlobalErrors.itemIDLessThanZero;
			}
			catch(NumberFormatException e){
				throw GlobalErrors.itemIDNotAnInteger;
			}
		}
		
		// If we made it here then everything is okay, pass the itemID
		return itemIDInt;
	}
	
	/**
	 * Gets the itemID parameter. Returns itemID if a positive integer, otherwise throws an error
	 * 
	 * @param request The request to read the parameter from
	 * @return int The nuber of items
	 * @throws Error
	 * @author Andy (andrewtu@cmu.edu, turner.andy@gmail.com)
	 */
	public static int checkAddressID(HttpServletRequest request) throws Error{
		String addressID = request.getParameter("addressID");
		int addressIDInt = 0;
		
		//See if the addressID is present
		if(addressID == null){
			throw GlobalErrors.addressIDNotPresent;
		}
		// See if the addressID is an empty string
		else if(addressID.equals(EMPTY)){
			throw GlobalErrors.addressIDEmpty;
		}
		else{
			// make sure the addressID is a number
			try{
				addressIDInt = Integer.parseInt(addressID);
				
				// make sure the addressID is positive
				if(addressIDInt <0)
					throw GlobalErrors.addressIDLessThanZero;
			}
			catch(NumberFormatException e){
				throw GlobalErrors.addressIDNotAnInteger;
			}
		}
		
		// If we made it here then everything is okay
		return addressIDInt;
	}
	
	public static int checkAccountID(HttpServletRequest request) throws Error{
		String accountID = request.getParameter("accountID");
		int accountIDInt = 0;
		
		//See if the accountID is present
		if(accountID == null){
			throw GlobalErrors.accountIDNotPresent;
		}
		// See if the accountID is an empty string
		else if(accountID.equals(EMPTY)){
			throw GlobalErrors.accountIDEmpty;
		}
		else{
			// make sure the accountID is a number
			try{
				accountIDInt = Integer.parseInt(accountID);
				
				// make sure the accountID is positive
				if(accountIDInt <0)
					throw GlobalErrors.accountIDLessThanZero;
			}
			catch(NumberFormatException e){
				throw GlobalErrors.accountIDNotAnInteger;
			}
		}
		
		// If we made it here then everything is okay
		return accountIDInt;
	}
	
	/**
	 * Gets the starting price amount. Returns the start price amount if present and is a positive float, otherwise throws an error
	 * 
	 * @param request The request to read the parameter from
	 * @return float The start price amount
	 * @throws Error
	 * @author Andy (andrewtu@cmu.edu, turner.andy@gmail.com)
	 */
	public static float checkStartPrice(HttpServletRequest request) throws Error{
		String startPrice = request.getParameter("startPrice");
		float startPriceF = 0.0f;
		
		//See if the start price is present
		if(startPrice == null){
			throw GlobalErrors.startPriceNotPresent;
		}
		// See if the start price is an empty string
		else if(startPrice.equals(EMPTY)){
			throw GlobalErrors.startPriceEmpty;
		}
		else{
			// make sure the start price is a number
			try{
				startPriceF = Float.parseFloat(startPrice);
				
				// make sure the start price is positive
				if(startPriceF < 0.0f)
					throw GlobalErrors.startPriceLessThanZero;
			}
			catch(NumberFormatException e){
				throw GlobalErrors.startPriceNotAFloat;
			}
		}
		
		// If we made it here then everything is okay
		return startPriceF;
	}
	
	/**
	 * Gets the starting price amount. Returns the start price amount if present and is a positive float, otherwise throws an error
	 * 
	 * @param request The request to read the parameter from
	 * @return float The start price amount
	 * @throws Error
	 * @author Andy (andrewtu@cmu.edu, turner.andy@gmail.com)
	 */
	public static float checkReservePrice(HttpServletRequest request) throws Error{
		String reservePrice = request.getParameter("reservePrice");
		float reservePriceF = 0.0f;
		
		//See if the reserve price is present
		if(reservePrice == null){
			throw GlobalErrors.reservePriceNotPresent;
		}
		// See if the reserve price is an empty string
		else if(reservePrice.equals(EMPTY)){
			throw GlobalErrors.reservePriceEmpty;
		}
		else{
			// make sure the reserve price is a number
			try{
				reservePriceF = Float.parseFloat(reservePrice);
				
				// make sure the reserve price is positive
				if(reservePriceF < 0.0f)
					throw GlobalErrors.reservePriceLessThanZero;
			}
			catch(NumberFormatException e){
				throw GlobalErrors.reservePriceNotAFloat;
			}
		}
		
		// If we made it here then everything is okay
		return reservePriceF;
	}
	
	/**
	 * Gets the starting price amount. Returns the start price amount if present and is a positive float, otherwise throws an error
	 * 
	 * @param request The request to read the parameter from
	 * @return float The start price amount
	 * @throws Error
	 * @author Andy (andrewtu@cmu.edu, turner.andy@gmail.com)
	 */
	public static float checkBuyNowPrice(HttpServletRequest request) throws Error{
		String buyNowPrice = request.getParameter("buyNowPrice");
		float buyNowPriceF = 0.0f;
		
		//See if the buy now price is present
		if(buyNowPrice == null){
			throw GlobalErrors.buyNowPriceNotPresent;
		}
		// See if the buy now price is an empty string
		else if(buyNowPrice.equals(EMPTY)){
			throw GlobalErrors.buyNowPriceEmpty;
		}
		else{
			// make sure the buy now price is a number
			try{
				buyNowPriceF = Float.parseFloat(buyNowPrice);
				
				// make sure the buy now price is positive
				if(buyNowPriceF < 0.0f)
					throw GlobalErrors.buyNowPriceLessThanZero;
			}
			catch(NumberFormatException e){
				throw GlobalErrors.buyNowPriceNotAFloat;
			}
		}
		
		// If we made it here then everything is okay
		return buyNowPriceF;
	}
	
	public static Date checkEndDate(HttpServletRequest request) throws Error{
		String endDate = request.getParameter("endDate");
		Date endDateD = null;
		
		//See if the end date is present
		if(endDate == null){
			throw GlobalErrors.endDateNotPresent;
		}
		// See if the end date price is an empty string
		else if(endDate.equals(EMPTY)){
			throw GlobalErrors.endDateEmpty;
		}
		else{
			// make sure the end date price is a real date
			try{
				endDateD = GlobalVars.sdfFull.parse(endDate);
				
				// make sure the end date is after now
				if(endDateD.before(Calendar.getInstance().getTime()))
					throw GlobalErrors.sellEndDateInPast;
			}
			catch(ParseException e){
				throw GlobalErrors.endDateInvalid;
			}
		}
		
		// If we made it here then everything is okay
		return endDateD;
	}
	
	public static Date checkExpirationDate(HttpServletRequest request) throws Error{
		String expirationDate = request.getParameter("expirationDate");
		Date expirationDateD = null;
		
		//See if the end date is present
		if(expirationDate == null){
			throw GlobalErrors.expirationDateNotPresent;
		}
		// See if the end date price is an empty string
		else if(expirationDate.equals(EMPTY)){
			throw GlobalErrors.expirationDateEmpty;
		}
		else{
			// make sure the expiration date is correct format
			try{
				expirationDateD = GlobalVars.sdfMMYY.parse(expirationDate);
				
				// make sure the end date is after now
				if(expirationDateD.before(Calendar.getInstance().getTime()))
					throw GlobalErrors.expirationDateInPast;
			}
			catch(ParseException e){
				throw GlobalErrors.expirationDateInvalid;
			}
		}
		
		// If we made it here then everything is okay
		return expirationDateD;
	}
	
	/**
	 * Reads a generic parameter from the requests and returns it as a String. If it is not present it returns blank
	 * 
	 * @param request The request to read the parameter from
	 * @param parameter The name of the parameter to read
	 * @return String of the parameter
	 * @author Andy (andrewtu@cmu.edu, turner.andy@gmail.com)
	 */
	public static String getParameter(HttpServletRequest request, String parameter){
		String retVal = null;
		
		retVal = request.getParameter(parameter);
		
		if(retVal != null)
			return retVal;
		else
			return new String();
	}
	
	/**
	 * Checks that the zip code is a valid US zip code. If not it throws an error.
	 * mmmmm... RegEx
	 * 
	 * @param request The request to read the parameter from
	 * @return String of the zip code if correct
	 * @throws Error
	 * @author Andy (andrewtu@cmu.edu, turner.andy@gmail.com)
	 */
	public static String checkZip(HttpServletRequest request) throws Error{
		String zip = request.getParameter("zip");
		
		//See if the zip is present
		if(zip == null){
			throw GlobalErrors.zipNotPresent;
		}
		// See if the zip is an empty string
		else if(zip.equals(EMPTY)){
			throw GlobalErrors.zipEmpty;
		}
		// See if the zip matches the regex for a zip code
		else if(!zipFormatCheck.matcher(zip).matches()){
			throw GlobalErrors.zipInvalid;
		}
		
		// If we made it here then everything is okay, pass the zip
		return zip;
	}
	
	/**
	 * Gets the state number selected by the user. Throws an exception if there is something wrong with the state
	 * 
	 * @param request The request to read the parameter from 
	 * @return String of the state number
	 * @throws Error
	 * @author Andy (andrewtu@cmu.edu, turner.andy@gmail.com)
	 */
	public static String checkState(HttpServletRequest request) throws Error{
		String stateNo = request.getParameter("state");
		
		//See if the state is present
		if(stateNo == null){
			throw GlobalErrors.stateNotPresent;
		}
		// See if the state is an empty string
		else if(stateNo.equals(EMPTY)){
			throw GlobalErrors.stateEmpty;
		}
		else{
			// make sure the state is a number
			try{
				int state = Integer.parseInt(stateNo);
				
				// make sure the state is positive
				if(state <0)
					throw GlobalErrors.stateLessThanZero;
				
				// If the state is zero it means they didn't select
				if(state==0)
					throw GlobalErrors.stateNotSelected;
				
				//TODO: check that the state really exists
			}
			catch(NumberFormatException e){
				throw GlobalErrors.stateNotAnInteger;
			}
		}
		
		// If we made it here then everything is okay, pass the state
		return stateNo;
	}
	
	public static int checkViewUserID(HttpServletRequest request) throws Error{
		String viewUserID = request.getParameter("viewUserID");
		int viewUserIDInt = 0;
		
		//See if the viewUserID is present
		if(viewUserID == null){
			throw GlobalErrors.viewUserIDNotPresent;
		}
		// See if the viewUserID is an empty string
		else if(viewUserID.equals(EMPTY)){
			throw GlobalErrors.viewUserIDEmpty;
		}
		else{
			// make sure the viewUserID is a number
			try{
				viewUserIDInt = Integer.parseInt(viewUserID);
				
				// make sure the viewUserID is positive
				if(viewUserIDInt <0)
					throw GlobalErrors.viewUserIDLessThanZero;
			}
			catch(NumberFormatException e){
				throw GlobalErrors.viewUserIDNotAnInteger;
			}
		}
		
		// If we made it here then everything is okay, pass the viewUserID back
		return viewUserIDInt;
	}
}
